package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.StrategyWifiMapper;
import com.kaili.basic.pojo.dto.MdmStrategyMainSetAddDto;
import com.kaili.basic.pojo.dto.MdmStrategyWifiAddDto;
import com.kaili.basic.pojo.dto.MdmStrategyWifiButtonDto;
import com.kaili.basic.pojo.entity.MdmStrategyMainEntity;
import com.kaili.basic.pojo.entity.MdmStrategyWifiEntity;
import com.kaili.basic.service.IStrategyWifiService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* WIFI配置 service实现类
* @author:zhangjl
* @Time: 2019-07-24
* @Copyright: ©  杭州凯立通信有限公司 版权所有
* @Warning: 本内容仅限于公司内部传阅,禁止外泄或用于其它商业目的
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class StrategyWifiServiceImpl extends ServiceImpl<StrategyWifiMapper, MdmStrategyWifiEntity>  implements IStrategyWifiService {

    @Override
    public boolean add(Integer id, MdmStrategyMainSetAddDto dto) {
       MdmStrategyWifiAddDto wifiAddDto = dto.getStrategyWifiDetail();
        List<MdmStrategyWifiButtonDto> list = wifiAddDto.getArr();
        for (MdmStrategyWifiButtonDto buttonDto :list){
            MdmStrategyWifiEntity wifiEntity = new MdmStrategyWifiEntity();
            BeanUtils.copyProperties(buttonDto,wifiEntity);
            wifiEntity.setStrategyId(id);
            this.insert(wifiEntity);
        }
        return true;
    }

    @Override
    public boolean modify(MdmStrategyMainEntity entity) {
        //1、删除设备策略
//        this.del(entity.getId());
        //2、新增设备类
        MdmStrategyWifiEntity wifiEntity = entity.getStrategyWifiDetail();
        List<MdmStrategyWifiButtonDto> list = wifiEntity.getArr();
        for (MdmStrategyWifiButtonDto buttonDto :list){
            BeanUtils.copyProperties(buttonDto,wifiEntity);
            wifiEntity.setStrategyId(entity.getId());
            Wrapper<MdmStrategyWifiEntity> wrapper = new EntityWrapper<>();
            wrapper.where("strategy_id ={0}", wifiEntity.getStrategyId());
            this.update(wifiEntity,wrapper);
        }
        return true;
    }

    @Override
    public boolean del(Integer id) {
        Wrapper<MdmStrategyWifiEntity> wrapper = new EntityWrapper<>();
        wrapper.where("strategy_id ={0}", id);
        this.delete(wrapper);
        return true;
    }
}
